<script setup>
import { getCurrentInstance, reactive } from 'vue'
import { useAllDataStore } from '@/stores'
import { useRouter } from 'vue-router'

const { proxy } = getCurrentInstance()
const store = useAllDataStore()
const router = useRouter()

const loginForm = reactive({
  username: '',
  password: '',
})

const handleLogin = async () => {
  try {
    const res = await proxy.$api.getMenu(loginForm)
    if (res && res.menuList) {
      store.updateMenuList(res.menuList)
      store.state.token = res.token
      store.addMenu(router)
      await router.push('/home')
    } else {
      proxy.$message.error('登录失败，未获取到菜单数据')
    }
  } catch (error) {
    console.error('登录请求异常:', error)
    proxy.$message.error('登录失败，请检查用户名或密码')
  }
}
</script>

<template>
  <div class="body-login">
    <el-form :Model="loginForm" class="login-container">
      <h3>欢迎登录</h3>
      <el-form-item>
        <el-input v-model="loginForm.username" placeholder="请输入用户名" type="text" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="loginForm.password" placeholder="请输入密码" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="less" scoped>
.body-login {
  width: 100%;
  height: 100%;
  background-image: url('../assets/images/bj.jpg');
  background-size: cover;
  overflow: hidden;

  .login-container {
    width: 400px;
    background-color: #fff;
    border: 1px solid #eaeaee;
    border-radius: 15px;
    padding: 35px 35px 15px 35px;
    box-shadow: 0 0 25px #cacaca; // 阴影
    margin: 320px auto;

    h3 {
      text-align: center;
      margin-bottom: 20px;
      color: #505050;
      font-weight: bold;
      font-size: 28px;
    }

    :deep(.el-form-item__content) {
      justify-content: center;
    }
  }
}
</style>
